package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        // 菜单表插入一条数据
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.add(dish);
        Long dishId = dish.getId();
        //
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            System.out.println("flavor = " + flavor);
        });
        // 将id 赋值到dish_id
        if(flavors != null && flavors.size() > 0)  {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
               // dishMapper.addFlavor(flavor);
            }
            dishFlavorMapper.addFlavor(flavors);
        }
    }
    @Override
    public PageResult listByPage(DishPageQueryDTO dishPageQueryDTO) {
       // return null;
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page <DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        Long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return new PageResult(total,records);
    }
    @Override
    public void delete(List<Long> ids) {
       // log.info("ids = " + ids);
        // 删除菜品表中的数据
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            // 判断菜品状态  可用状态不能删除
            if(dish.getStatus() == StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
          //  dishMapper.deleteById(id);
        }
        List<Long> selectedIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(selectedIds != null && selectedIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 删除口味表中的数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        // 查出一个菜品
        Dish dish = dishMapper.getById(id);
        // 查出多个口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
      //  log.info(dish);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
        if(flavors != null && flavors.size() > 0){
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.addFlavor(flavors);
        }
    }
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVolist = new ArrayList<>();
        for (Dish d: dishList){
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            List<DishFlavor> flavors =  dishFlavorMapper.getByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVolist.add(dishVO);
        }
        return dishVolist;
    }
}
